package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 购物车控制层
 * @author Javid Xi
 * @version 1.0
 * @since 2021/7/22
 */
@RestController
@RequestMapping("/shopping")
@Api(tags = "CartController", description = "购物车控制层")
public class CartController {

    @Reference(timeout = 3000,check = false)
    ICartService cartService;

    public static String USER_INFO_KEY = "userInfo";

    /**
     * 获取购物车数据
     * @param request 请求
     * @return ResponseData
     */
    @GetMapping("/carts")
    public ResponseData<?> getCarts(HttpServletRequest request) {
        //获取用户信息
        String userInfo = (String) request.getAttribute(USER_INFO_KEY);
        Map<String,Object> parse = (Map<String, Object>) JSON.parse(userInfo);
        Long userId = Long.valueOf(parse.get("uid").toString());

        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(userId);

        CartListByIdResponse response = cartService.getCartListById(cartListByIdRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(response.getCartProductDtos());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 添加购物车
     * @param map 请求参数
     * @return ResponseData
     */
    @PostMapping("/carts")
    public ResponseData<?> addCarts(@RequestBody Map<String,Integer> map) {
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId((long)map.get("userId"));
        addCartRequest.setItemId((long)map.get("productId"));
        addCartRequest.setNum(map.get("productNum"));

        AddCartResponse response = cartService.addToCart(addCartRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(response.getMsg());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 更新购物车中商品
     * @param map 请求参数
     * @return ResponseData
     */
    @PutMapping("/carts")
    public ResponseData<?> updateCarts(@RequestBody Map<String,String> map ) {

        UpdateCartNumRequest updateCartNumRequest = new UpdateCartNumRequest();
        updateCartNumRequest.setChecked(map.get("checked"));
        updateCartNumRequest.setItemId(Long.valueOf(map.get("productId")));
        updateCartNumRequest.setNum(Integer.valueOf(map.get("productNum")));
        updateCartNumRequest.setUserId(Long.valueOf(map.get("userId")));

        UpdateCartNumResponse response = cartService.updateCartNum(updateCartNumRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(response.getMsg());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 删除购物车某件中商品
     * @param uid 用户id
     * @param pid 商品id
     * @return ResponseData
     */
    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData<?> deleteCarts(@PathVariable("uid")Long uid, @PathVariable("pid")Long pid) {
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);

        DeleteCartItemResponse response = cartService.deleteCartItem(deleteCartItemRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode()))
            return new ResponseUtil<>().setData(response.getMsg());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 删除选中商品
     * @param id 用户id
     * @return ResponseData
     */
    @DeleteMapping("/items/{id}")
    public ResponseData<?> deleteCarts(@PathVariable("id")Long id) {
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(id);

        DeleteCheckedItemResponse response = cartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(response.getMsg());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 选择购物车中的所有商品
     * @param map 请求参数
     * @return ResponseData
     */
    @PutMapping("/items")
    public ResponseData<?> checkAllCartItem(@RequestBody Map<String,String> map) {
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        checkAllItemRequest.setUserId(Long.valueOf(map.get("userId")));
        checkAllItemRequest.setChecked(map.get("checked"));

        CheckAllItemResponse response = cartService.checkAllCartItem(checkAllItemRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(response.getMsg());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }
}
